<?php
/**
 * service.php
 * User: yzm
 * Date: 2017/9/18
 */
class Service extends WebBase{
    public $unreply_num;

    public $check_login_id;
    public $session_login_name;

    public $u_acid;

    public function __construct() {
        global $_W, $_GPC;
        //未回复工单数量
        $this->unreply_num = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('wherein_knowledge_work_order') . " WHERE `uniacid` = :uniacid AND `parent_id` = 0 AND `customer_status` IN (1,3) ", array(':uniacid' => $_W['uniacid']));
        $this->check_login_id = substr(md5(md5('knowledge')), 0, 5);
        $this->session_login_name = substr(md5('login_type'), 0, 5);

        $arr = array('knowledge', 'orders', 'question');
        if (in_array($_GPC['act'], $arr)) {
            $this->checkLogin();
        }

        if (!empty($_W['uniacid'])) {
            $_SESSION['uniacid'] = $_W['uniacid'];
        }

    }


    public function index() {
        global $_GPC, $_W;
//        header("Location: ". $this->createWebUrl('service', array('act' => 'knowledge')));
        if ($_SESSION[$this->session_login_name] != $_SESSION['userinfo']['openid'].$this->check_login_id) {
            $url = urlencode(getSite('redirect_url') . '/web/' . $this->createWebUrl('service', array('act' => 'backLogin', 'uniacid' => $_W['uniacid'])));

            $state = md5(mt_rand(1000, 9999));
            include $this->template('login');
        } else {
            header('Location: ' . $this->createWebUrl('service', array('act' => 'knowledge')));
        }
    }

    /*
     * 知识库首页
     */
    public function knowledge() {
        global $_GPC,$_W;

        //一级分类列表
        $category_list = pdo_getall('wherein_knowledge_category', array('uniacid' => $_W['uniacid'], 'parent_id' => '0'), array('cat_id', 'cat_name', 'sort_order'), '', array('sort_order DESC'));

        //二级分类
        if (!isset($_GPC['cat_id']) || $_GPC['cat_id'] == 0) {
            $category_child_list = pdo_fetchall("SELECT cat_id, cat_name FROM " . tablename('wherein_knowledge_category') . " WHERE `uniacid`=:uniacid AND `parent_id` <> 0", array(':uniacid' => $_W['uniacid']));
        } else {
            $category_child_list = pdo_getall('wherein_knowledge_category', array('uniacid' => $_W['uniacid'], 'parent_id' => $_GPC['cat_id']), array('cat_id', 'cat_name', 'sort_order'), '', array('sort_order DESC'));
        }

        //所有标签--用于筛选
        $label_list = pdo_getall('wherein_knowledge_label', array('uniacid' => $_W['uniacid']), array('label_id', 'label_name'));

        if ($_W['isajax']) {
            $where_sql = ' WHERE `uniacid`=:uniacid ';
            $where[':uniacid'] = $_W['uniacid'];
            //符合问题的id集合
            $question_ids = array();

            /* 一级分类: 判断当前分类下是否有二级分类, 查询相关问题: 条件 一级分类+二级分类(如果有) */
            if (!empty($_GPC['cat_id']) && !isset($_GPC['cat_ids'])) {
                //查询当下有没有子分类
                $child_list = pdo_getall('wherein_knowledge_category', array('uniacid' => $_W['uniacid'], 'parent_id' => $_GPC['cat_id']), array('cat_id'));
                $child_list = empty($child_list) ? array() : return_array_value($child_list, 'cat_id');
                array_push($child_list, $_GPC['cat_id']);
                $child_list_implode = implode(',', $child_list);
                //查询问题id
                $question_list = pdo_fetchall("SELECT question_id FROM " . tablename('wherein_knowledge_question') . " WHERE `uniacid` = :uniacid AND `cat_id` IN (".$child_list_implode.")", array(':uniacid' => $_W['uniacid']));
                if (!empty($question_list)) {
                    $question_list = return_array_value($question_list, 'question_id');
                    foreach ($question_list as $v) {
                        array_push($question_ids, $v);
                    }
                    unset($question_list);//销毁
                } else {
                    array_push($question_ids, '0');
                }
            }

            /* 搜索-二级分类 */
            if (!empty($_GPC['cat_ids'])) {
                $need_list = array();
                $cat_ids = implode(',', $_GPC['cat_ids']);
                $question_list = pdo_fetchall("SELECT `question_id` FROM " . tablename('wherein_knowledge_question') . " WHERE `uniacid` = :uniacid AND `cat_id` IN (" . $cat_ids . ")", array(':uniacid' => $_W['uniacid']));
                if (!empty($question_list)) {
                    $question_list = return_array_value($question_list, 'question_id');
                    /* 搜索-标签 */
                    if (!empty($_GPC['label_ids'])) {
                        $label_ids = implode(',', $_GPC['label_ids']);
                        $label_question_list = pdo_fetchall("SELECT DISTINCT(`question_id`) FROM " . tablename('wherein_knowledge_relation') . " WHERE `uniacid` = :uniacid AND `label_id` IN (" . $label_ids . ")", array(':uniacid' => $_W['uniacid']));
                        $label_question_list = return_array_value($label_question_list, 'question_id');
                        $diff_list = array_intersect($question_list, $label_question_list);
                        if (!empty($diff_list)) {
                            $need_list = $diff_list;
                        }
                    } else {
                        $need_list = $question_list;
                    }
                }
                if (!empty($need_list)) {
                    foreach ($need_list as $v) {
                        array_push($question_ids, $v);
                    }
                } else {
                    array_push($question_ids, 0);
                }
            }

            /* 搜索-标签 */
            if (empty($_GPC['cat_ids']) && !empty($_GPC['label_ids'])) {
                $label_ids = implode(',', $_GPC['label_ids']);
                $question_list = pdo_fetchall("SELECT DISTINCT(`question_id`) FROM " . tablename('wherein_knowledge_relation') . " WHERE `uniacid` = :uniacid AND `label_id` IN (" . $label_ids . ")", array(':uniacid' => $_W['uniacid']));
                if (!empty($question_list)) {
                    $question_list = return_array_value($question_list, 'question_id');
                    foreach ($question_list as $v) {
                        array_push($question_ids, $v);
                    }
                } else {
                    array_push($question_ids, '0');
                }
            }

            /* 搜索-问题 */
            if (!empty($_GPC['question'])) {
                //用中文分词
                /*$question_list = $this->searchQuestion($_GPC['question']);
                if (!empty($question_list)) {
                    $question_list = return_array_value($question_list, 'question_id');
                    foreach ($question_list as $v) {
                        array_push($question_ids, $v);
                    }
                } else {
                }*/
                //模糊查询
                $where_sql .= " AND (`question` LIKE :question OR `similar_question` LIKE :similar_question)";
                $where[':question'] = '%' . $_GPC['question'] . '%';
                $where[':similar_question'] = '%' . $_GPC['question'] . '%';
            }

            if (!empty($question_ids)) {
                $question_ids = array_unique($question_ids);
                $where_sql .= ' AND `question_id` IN (' . implode(',', $question_ids) . ')';
            }

            $total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('wherein_knowledge_question') . $where_sql, $where);

            //分页数据
            $limit = 5;
            $page = !empty($_GPC['page']) && isset($_GPC['page']) ? intval($_GPC['page']) : 1;
            $max_page = ceil($total/$limit) < 1 ? $page : ceil($total/$limit);
            if ($page < 1) $page = 1;
            if ($page > $max_page) $page = $max_page;
            $offset = ($page - 1) * $limit;

            $list = pdo_fetchall("SELECT * FROM ".tablename('wherein_knowledge_question') . $where_sql . " ORDER BY `question_id` DESC LIMIT " . $offset . "," . $limit, $where);
            if (!empty($list)) {
                foreach ($list as $key => $value) {
                    $list[$key]['cat_name'] = pdo_getcolumn('wherein_knowledge_category', array('cat_id' => $value['cat_id']), 'cat_name');
                    $label_name_list = pdo_fetchall("SELECT l.label_name FROM " . tablename('wherein_knowledge_label') . " AS l LEFT JOIN " . tablename('wherein_knowledge_relation') . " AS r ON r.label_id = l.label_id WHERE r.question_id = " . $value['question_id']);
                    if (!empty($label_name_list)) {
                        $label_name_list = return_array_value($label_name_list, 'label_name');
                        $label_name_list = implode('、', $label_name_list);
                    } else {
                        $label_name_list = '无';
                    }
                    $list[$key]['label_name'] = $label_name_list;
                    $list[$key]['answer_all'] = htmlspecialchars_decode($value['answer']);
                    $list[$key]['answer'] = strip_tags(htmlspecialchars_decode($value['answer']));
                }
            }
            $out['pages'] = array('total' => $total, 'limit' => $limit, 'page' => $page);
            $out['question_list'] = $list;
            $this->outjson($out);

        }

        $unreply_num = $this->unreply_num;
        include $this->template('knowledge');
    }

    /*
     * 工单管理
     */
    public function orders() {
        global $_W,$_GPC;

        //所有一级的分类
        $list = pdo_getall('wherein_knowledge_work_order', array('uniacid' => $_W['uniacid'], 'belong_to' => 0, 'parent_id' => 0), array('cat_id', 'child_cat_id'));

        $category_list = array();
        if (!empty($list)) {
            $cat_list_id = $child_cat_list_id = array();
            foreach ($list as $key => $value) {
                $cat_list_id[] = $value['cat_id'];
                $child_cat_list_id[] = $value['child_cat_id'];
            }

            if (!empty($cat_list_id)) {
                $cat_list = pdo_fetchall("SELECT cat_id,cat_name,parent_id FROM " . tablename('wherein_knowledge_category') . " WHERE `cat_id` IN (" . implode(',', $cat_list_id) . ")");
                foreach ($cat_list as $key => $value) {
                    $category_list[$value['cat_id']] = $value;
                    $category_list[$value['cat_id']]['child_list'] = array();
                }
                if (!empty($child_cat_list_id)) {
                    $child_cat_list = pdo_fetchall("SELECT cat_id,cat_name,parent_id FROM " . tablename('wherein_knowledge_category') . " WHERE `cat_id` IN (" . implode(',', $child_cat_list_id) . ")");
                    foreach ($child_cat_list as $v) {
                        $category_list[$v['parent_id']]['child_list'][] = $v;
                    }
                }
            }
        }

        if ($_W['isajax']) {

            $where_sql = ' WHERE `uniacid`=:uniacid ' . ' AND `belong_to` = :belong_to AND `order_status` = :order_status ';
            $where[':uniacid'] = $_W['uniacid'];
            $where[':belong_to'] = 0;
            $where[':order_status'] = $_GPC['order_status'];

            $sortOrder = !empty($_GPC['sortOrder']) ? $_GPC['sortOrder'] : 'desc';
            $sortName = !empty($_GPC['sortName']) ? $_GPC['sortName'] : 'update_time DESC, add_time ';

            //搜索-分类
            if (!empty($_GPC['cat_id'])) {
                //查询当下有没有子分类
                if ($_GPC['cat_type'] == 0) {
                    $where_sql .= " AND `cat_id` = " . $_GPC['cat_id'];
                } else {
                    $where_sql .= " AND `child_cat_id` = " . $_GPC['cat_id'];
                }
            }
            //搜索-问题/关键词
            if (!empty($_GPC['search'])) {
                $where_sql .= " AND (`order_sn` LIKE :search OR `nickname` LIKE :search) ";
                $where[':search'] = '%' . $_GPC['search'] . '%';
            }
            //搜索-时间
            if (!empty($_GPC['add_time'])) {
                $where_sql .= " AND (`add_time` >= :add_time AND `add_time` <= :end_time)";
                $where[':add_time'] = strtotime($_GPC['add_time']);
                $where[':end_time'] = $where[':add_time'] + 24 * 60 * 60;
            }

            $total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('wherein_knowledge_work_order') . $where_sql, $where);

            //分页数据
            $limit = $_GPC['limit'];
            $page = !empty($_GPC['page']) && isset($_GPC['page']) ? intval($_GPC['page']) : 1;
            $max_page = ceil($total/$limit) < 1 ? $page : ceil($total/$limit);
            if ($page < 1) $page = 1;
            if ($page > $max_page) $page = $max_page;
            $offset = ($page - 1) * $limit;

            $list = pdo_fetchall("SELECT * FROM ".tablename('wherein_knowledge_work_order') . $where_sql . " ORDER BY " . $sortName . " " . $sortOrder . " LIMIT " . $offset . ", " . $limit, $where);
            if (!empty($list)) {
                load()->model('mc');
                foreach ($list as $key => $value) {
                    $list[$key]['order_status_name'] = $this->order_status[$value['order_status']];
                    $list[$key]['customer_status_name'] = $this->customer_status[$value['customer_status']];
                    $list[$key]['add_time'] = format($value['add_time']);
                    $list[$key]['update_time'] = !empty($value['update_time']) ? format($value['update_time']) : '-';
                    $user = mc_fansinfo($value['openid']);
                    $list[$key]['avatar'] = !empty($user['avatar']) ? $user['avatar'] : NO_AVATAR;
                }
            }

            $out['code'] = 0;
            $out['msg'] = '成功';
            $out['count'] = $total;
            $out['data'] = $list;
            $this->outjson($out);
        }

        $unreply_num = $this->unreply_num;

        include $this->template('orders');
    }

    /*
     * 常见问题
     */
    public function question() {
        global $_W,$_GPC;

        $where_sql = ' WHERE `uniacid`=:uniacid AND `is_common`=:is_common ';
        $where[':uniacid'] = $_W['uniacid'];
        $where[':is_common'] = 1;

        /*
         * 查询分类下存在常见问题的分类
         * 1.查询所有常见问题对应的分类id; 2获取分类id的信息; 3分拆是一级分类还是子分类; 4根据3分拆出的子分类查询出对应的父类; 5组合父类与子类信息
         */
        //初始化变量
        $category_list = $cate_list = $child_list = $parent_id_list = array();
        //查询所有的常见分类id
        $cat_ids_list = pdo_fetchall("SELECT cat_id FROM " . tablename('wherein_knowledge_question') . $where_sql, $where, 'cat_id');
        if (!empty($cat_ids_list)) {
            $cat_ids_list = return_array_value($cat_ids_list, 'cat_id');
            $cat_ids_str = implode(',', $cat_ids_list);
            //查询出来,数据里面包含有一级或者二级分类
            $cat_data = pdo_fetchall("SELECT cat_id,cat_name,parent_id FROM " . tablename('wherein_knowledge_category') . " WHERE `cat_id` IN (" . $cat_ids_str . ")");
            foreach ($cat_data as $key => $value) {
                if ($value['parent_id'] == 0) {
                    $cate_list[] = $value;
                } else {
                    $child_list[] = $value;
                    $parent_id_list[] = $value['parent_id'];
                }
            }
            if (!empty($parent_id_list)) {
                $parent_cat_data = pdo_fetchall("SELECT cat_id,cat_name,parent_id FROM " . tablename('wherein_knowledge_category') . " WHERE `cat_id` IN (" . implode(',', $parent_id_list) . ")");
                foreach ($parent_cat_data as $v) {
                    array_push($cate_list, $v);
                }
            }
            foreach ($cate_list as $key => $value) {
                $category_list[$value['cat_id']] = $value;
                $child_list_arr = array();
                $category_list[$value['cat_id']]['child_list'] = $child_list_arr;
            }
            foreach ($child_list as $v) {
                $category_list[$v['parent_id']]['child_list'][] = $v;
            }
        }

        if ($_W['isajax']) {
            //符合问题的id集合
            $question_ids = array();

            /* 一级分类: 判断当前分类下是否有二级分类, 查询相关问题: 条件 一级分类+二级分类(如果有) */
            if (!empty($_GPC['cat_id'])) {
                //查询当下有没有子分类
                $child_list = pdo_getall('wherein_knowledge_category', array('uniacid' => $_W['uniacid'], 'parent_id' => $_GPC['cat_id']), array('cat_id'));
                $child_list = empty($child_list) ? array() : return_array_value($child_list, 'cat_id');
                array_push($child_list, $_GPC['cat_id']);
                $child_list_implode = implode(',', $child_list);
                //查询问题id
                $question_list = pdo_fetchall("SELECT question_id FROM " . tablename('wherein_knowledge_question') . " WHERE `uniacid` = :uniacid AND `cat_id` IN (".$child_list_implode.")", array(':uniacid' => $_W['uniacid']));
                if (!empty($question_list)) {
                    $question_list = return_array_value($question_list, 'question_id');
                    foreach ($question_list as $v) {
                        array_push($question_ids, $v);
                    }
                    unset($question_list);//销毁
                } else {
                    array_push($question_ids, '0');
                }
            }

            /* 搜索-问题 */
            if (!empty($_GPC['question'])) {
                //用中文分词
                /*$question_list = $this->searchQuestion($_GPC['question']);
                if (!empty($question_list)) {
                    $question_list = return_array_value($question_list, 'question_id');
                    foreach ($question_list as $v) {
                        array_push($question_ids, $v);
                    }
                } else {
                }*/
                //模糊查询
                $where_sql .= " AND (`question` LIKE :question OR `similar_question` LIKE :similar_question)";
                $where[':question'] = '%' . $_GPC['question'] . '%';
                $where[':similar_question'] = '%' . $_GPC['question'] . '%';
            }
            if (!empty($question_ids)) {
                $question_ids = array_unique($question_ids);
                $where_sql .= ' AND `question_id` IN (' . implode(',', $question_ids) . ')';
            }

            //总数
            $total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('wherein_knowledge_question') . $where_sql, $where);
            //分页数据
            $limit = 5;
            $page = !empty($_GPC['page']) && isset($_GPC['page']) ? intval($_GPC['page']) : 1;
            $max_page = ceil($total/$limit) < 1 ? $page : ceil($total/$limit);
            if ($page < 1) $page = 1;
            if ($page > $max_page) $page = $max_page;
            $offset = ($page - 1) * $limit;

            $list = pdo_fetchall("SELECT * FROM ".tablename('wherein_knowledge_question') . $where_sql . " ORDER BY `question_id` DESC LIMIT " . $offset . ", " . $limit, $where);
            if (!empty($list)) {
                foreach ($list as $key => $value) {
                    $list[$key]['cat_name'] = pdo_getcolumn('wherein_knowledge_category', array('cat_id' => $value['cat_id']), 'cat_name');
                    $label_name_list = pdo_fetchall("SELECT l.label_name FROM " . tablename('wherein_knowledge_label') . " AS l LEFT JOIN " . tablename('wherein_knowledge_relation') . " AS r ON r.label_id = l.label_id WHERE r.question_id = " . $value['question_id']);
                    if (!empty($label_name_list)) {
                        $label_name_list = return_array_value($label_name_list, 'label_name');
                        $label_name_list = implode('、', $label_name_list);
                    } else {
                        $label_name_list = '无';
                    }
                    $list[$key]['label_name'] = $label_name_list;
                    $list[$key]['answer_all'] = htmlspecialchars_decode($value['answer']);
                    $list[$key]['answer'] = strip_tags(htmlspecialchars_decode($value['answer']));
                }
            }

            if (!empty($_GPC['cat_id'])) {
                $out['cat_name'] = pdo_getcolumn('wherein_knowledge_category', array('cat_id' => $_GPC['cat_id']), 'cat_name');
            } else {
                $out['cat_name'] = '全部';
            }
            $out['pages'] = array('total' => $total, 'limit' => $limit, 'page' => $page);
            $out['question_list'] = $list;
            $this->outjson($out);
        }

        $unreply_num = $this->unreply_num;
        include $this->template('question');
    }

    /*
     * 获取分类管理数据
     */
    public function getCateList() {
        global $_W;
        //分类管理数据 一级分类和二级分类
        $category_list = $category_child_list = array();
        $category_list = pdo_getall('wherein_knowledge_category', array('uniacid' => $_W['uniacid'], 'parent_id' => '0'), array('cat_id', 'cat_name', 'sort_order'), '', array('sort_order DESC'));
        foreach ($category_list as $k => $v) {
            $child_list = pdo_getall('wherein_knowledge_category', array('uniacid' => $_W['uniacid'], 'parent_id' => $v['cat_id']), array('cat_id', 'cat_name', 'parent_id', 'sort_order'), '', array('sort_order DESC'));
            $category_list[$k] = array(
                'cat_id' => $v['cat_id'],
                'cat_name' => $v['cat_name'],
                'sort_order' => $v['sort_order'],
                'child_list' => $child_list
            );
            unset($child_list);
        }
        return empty($category_list) ? array() : $category_list;
    }

    /*
     * 返回
     */
    public function backLogin() {
        global $_W,$_GPC;

        //允许授权.返回带上code和state参数
        //禁止授权,返回只有state参数
        if (isset($_GPC['code']) && !empty($_GPC['code'])) {
            /* 获取用户的openid 查询db看openid的状态是否可登录 */
            load()->classs('weixin.platform');
            $get_access_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . getSite('appid') . '&secret=' . getSite('secret') . '&code=' . $_GPC['code'] . '&grant_type=authorization_code';
            $result = file_get_contents($get_access_token_url);//return string
            $result = json_decode($result, true);//json转数组
            if (isset($result['errcode'])) {
                message($result['errmsg'].'错误码: ' . $result['errcode'], $this->createWebUrl('service', array('act' => 'index')), 'error');
            } else {
                $openid = $result['openid'];
                load()->model('mc');
                $res = pdo_get('wherein_knowledge_employee', array('uniacid' => $_W['uniacid'], 'openid' => $openid, 'status' => 1), array('employee_id'));
                if ($res) {
                    $_W['openid'] = $openid;
                    $_SESSION[$this->session_login_name] = $openid.$this->check_login_id;
                    $user = mc_fansinfo($openid);
                    $u = array();
                    if (!empty($user)) {
                        $u['openid'] = $openid;
                        $u['avatar'] = $user['avatar'];
                        $u['nickname'] = $user['nickname'];
                    }
                    $_SESSION['userinfo'] = $u;

                    message('登录成功!', $this->createWebUrl('service', array('act' => 'knowledge')), 'success');
                } else {
                    message('禁止登陆: 可能还没被授权', $this->createWebUrl('service', array('act' => 'index', 'uniacid' => $_W['uniacid'])), 'error');
                }
            }
        } else {
            header('Location: ' . $this->createWebUrl('service', array('act' => 'index', 'uniacid' => $_W['uniacid'])));
        }
    }

    /*
     * 检查登录
     */
    public function checkLogin() {
        global $_W;
        if ($_SESSION[$this->session_login_name] != $_SESSION['userinfo']['openid'].$this->check_login_id) {
            header('Location: ' . $this->createWebUrl('service', array('act' => 'index', 'uniacid' => $_W['uniacid'])));
        }
    }

    /*
     * 退出登录
     */
    public function loginout() {
        global $_W;
        unset($_SESSION[$this->session_login_name]);
        unset($_SESSION['userinfo']);
        $uniacid = $_SESSION['uniacid'];
        unset($_SESSION['uniacid']);
        message('退出成功', $this->createWebUrl('service', array('act' => 'index', 'uniacid' => $uniacid)), 'success');
    }

    /*
     * 获取工单数据
     */
    public function getWorkOrder() {
        global $_W,$_GPC;
        $order_id = !empty($_GPC['order_id']) ? $_GPC['order_id'] : 0;
        if (empty($order_id)) {
            $this->outjson(array('code' => 404, 'msg' => '参数错误'));
        }
        //获取工单数据
        $order_data = pdo_get('wherein_knowledge_work_order', array('uniacid' => $_W['uniacid'], 'order_id' => $order_id, 'belong_to' => 0, 'parent_id' => 0), array('order_id', 'parent_id', 'cat_id', 'child_cat_id', 'openid', 'nickname', 'content', 'pic', 'add_time', 'order_status', 'customer_status', 'is_reply'));
        if (empty($order_data)) {
            $this->outjson(array('code' => 404, 'msg' => '找不到可编辑的数据'));
        }
        $order_data['cat_name'] = pdo_getcolumn('wherein_knowledge_category', array('cat_id' => $order_data['cat_id']), 'cat_name');
        $order_data['child_cat_name'] = empty($order_data['child_cat_id']) ? '' : pdo_getcolumn('wherein_knowledge_category', array('cat_id' => $order_data['child_cat_id']), 'cat_name');
        load()->model('mc');
        $user = mc_fansinfo($order_data['openid']);
        $order_data['avatar'] = !empty($user['avatar']) ? $user['avatar'] : NO_AVATAR;
        $order_data['add_time'] = format($order_data['add_time'], 'Y-m-d H:i');
        $order_data['customer_status_name'] = $this->customer_status[$order_data['customer_status']];
        //获取用户最新提问的消息
        $new_answer_data = pdo_fetch("SELECT content,add_time FROM " . tablename('wherein_knowledge_work_order') . " WHERE belong_to = :belong_to AND is_reply = :is_reply ORDER BY order_id DESC ", array(':belong_to' => $order_data['order_id'], ':is_reply' => 0));
        $order_data['new_content'] = !empty($new_answer_data) ? $new_answer_data['content'] : $order_data['content'];
        $order_data['new_add_time'] = !empty($new_answer_data) ? format($new_answer_data['add_time'], 'Y-m-d H:i') : $order_data['add_time'];
        if (!empty($new_answer_data['pic'])) {
            $order_data['new_pic'] = explode(IMG_IMPLODE, $new_answer_data['pic']);
            foreach ($order_data['new_pic'] as $k=>$v) {
                $order_data['new_pic'][$k] = tomedia($v);
            }
        } else {
            $order_data['new_pic'] = array();
            if (!empty($order_data['pic'])) {
                $order_data['new_pic'] = explode(IMG_IMPLODE, $order_data['pic']);
                foreach ($order_data['new_pic'] as $k=>$v) {
                    $order_data['new_pic'][$k] = tomedia($v);
                }
            }
        }

        $order_list = array();
        $order_list[] = array(
            'order_id' => $order_data['order_id'],
            'belong_to' => 0,
            'parent_id' => $order_data['parent_id'],
            'openid' => $order_data['openid'],
            'nickname' => $order_data['nickname'],
            'content' => $order_data['content'],
            'pic' => $order_data['pic'],
            'add_time' => $order_data['add_time'],
            'is_reply' => $order_data['is_reply'],
        );

        //查询相关的工单回复信息
        $order_reply_list = pdo_fetchall("SELECT order_id,belong_to,parent_id,openid,nickname,content,pic,add_time,is_reply FROM " . tablename('wherein_knowledge_work_order') . " WHERE uniacid = :uniacid AND belong_to = :belong_to ORDER BY order_id asc", array(':uniacid' => $_W['uniacid'], ':belong_to' => $_GPC['order_id']));
        if (!empty($order_reply_list)) {
            foreach ($order_reply_list as $key => $value) {
                $value['add_time'] = format($value['add_time'], 'Y-m-d H:i');
                $order_list[] = $value;
            }
        }
        foreach ($order_list as $key => $value) {
            $user = mc_fansinfo($value['openid']);
            $order_list[$key]['avatar'] = !empty($user['avatar']) ? $user['avatar'] : NO_AVATAR;
            $order_list[$key]['content'] = htmlspecialchars_decode($value['content']);
            $order_list[$key]['pic'] = array();
            if (!empty($value['pic'])) {
                $order_list[$key]['pic'] = explode(IMG_IMPLODE, $value['pic']);
                foreach ($order_list[$key]['pic'] as $k=>$v) {
                    $order_list[$key]['pic'][$k] = tomedia($v);
                }
            }
        }

        $out['code'] = 200;
        $out['info'] = $order_data;
        $out['order_list'] = $order_list;
        $this->outjson($out);
    }
}